package com.treetao.union.db.service.impl;

import com.treetao.union.db.model.MallCouponRecord;
import com.treetao.union.db.mapper.MallCouponRecordMapper;
import com.treetao.union.db.service.MallCouponRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 优惠券领取使用记录 服务实现类
 * </p>
 *
 * @author Dev.M
 * @since 2020-05-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MallCouponRecordServiceImpl extends ServiceImpl<MallCouponRecordMapper, MallCouponRecord>
		implements MallCouponRecordService {

	@Override
	public List<MallCouponRecord> getCouponRecord(Long userId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("userId", userId);
		return list(q);
	}

	@Override
	public List<MallCouponRecord> getAvaliableCouponRecord(Long userId,Double maxMan) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("used", false);
		q.eq("userId", userId);
		
		//最大满金额
		if(maxMan!=null) {
			q.le("man", maxMan);
		}
		
		//在使用范围内
		Date cur = new Date();
		q.ge("invalidEndTime", cur);
		
		//按满金额排序 
		q.orderByDesc("man");
		return list(q);
	}

	@Override
	public List<MallCouponRecord> getUsedCouponRecord(Long userId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("used", true);
		q.eq("userId", userId);
		return list(q);
	}

	@Override
	public List<MallCouponRecord> getOverdateCouponRecord(Long userId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("used", false);
		q.eq("userId", userId);
		//超过了范围内
		Date cur = new Date();
		q.le("invalidEndTime", cur);
		return list(q);
	}

	@Override
	public boolean isReceivedCoupon(Long userId, Long couponId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("userId", userId);
		q.eq("couponId", couponId);
		return count(q) > 0;
	}

	@Override
	public int getReceivedNumt(Long couponId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("couponId", couponId);
		return count(q);
	}

	@Override
	public boolean setCouponUsed(Long userId, Long couponId) {
		MallCouponRecord record = getMyCoupon(userId, couponId);
		if(record!=null) {
			record.setUsed(true);
			record.setUsedTime(new Date());
			return updateById(record);
		}
		return false;
	}

	@Override
	public MallCouponRecord getMyCoupon(Long userId, Long couponId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("del_flag", false);
		q.eq("userId", userId);
		q.eq("couponId", couponId);
		return getOne(q);
	}

	@Override
	public int getUsedNum(Long couponId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("couponId", couponId);
		q.eq("used", true);
		return count(q);
	}

	@Override
	public int getOverdatedNum(Long couponId) {
		QueryWrapper<MallCouponRecord> q = new QueryWrapper<MallCouponRecord>();
		q.eq("couponId", couponId);
		q.eq("used", false);
		q.le("invalidEndTime", new Date());
		return count(q);
	}

}
